/**
 * 
 */
package site.service;

import java.sql.Connection;
import java.util.List;

import neoutil.CommonService;
import neoutil.DaoSupport;
import site.dao.CategoryDao;
import site.entity.Category;

/**
 * @author Neo
 * 
 */
public class CategoryRepository extends CommonService {

	public void saveCategory(Category category) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			categoryDao.save(category);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection(connection);
		}
	}

	public List<Category> getCategories(int cid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE parentCid=(SELECT parentCid FROM category WHERE id=?)";

			return categoryDao.find(sql, new Object[] { cid });
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			closeConnection(connection);
		}
	}

	public Category getCategoryByCid(int cid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE cid=?";

			return categoryDao.find(sql, new Object[] { cid }).get(0);
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			closeConnection(connection);
		}
	}

	public static void main(String[] args) {

	}

}
